package de.fhbrs.migration;

import java.io.*;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * With this Class it is possible to migrate all data
 * from the MySQL DB into Fedora.
 * 
 * @version $Id: Migration.java 379 2008-01-17 15:27:36Z andreas.moser.bm $
 * @author 	Andreas Moser
 * 
 * 001 10.12.2007 - Andreas Moser - Added all Standard Comments
 * 
 */
public class Migration {
	
	public static void main(String[] args) {
		
		
		if (args.length<8){
			System.out.println("ERROR The command you used is invalid!");
			System.out.println("Please use the following command:");
			System.out.println("java -jar LitsearchFedoraMigration.jar <mysqlHost> <mysqlDB> <mysqlUser> <mysqlPass> <fedoraHost> <fedoraPort> <fedoraUser> <fedoraPass>");
			System.out.println("Example:");
			System.out.println("java -jar LitsearchFedoraMigration.jar localhost litsearch root admin localhost 8080 fedoraAdmin admin");
			return;
		}
		String mysqlHostname = args[0];
		String mysqlDatabase = args[1];
		String mysqlUsername = args[2];
		String mysqlPassword = args[3];
		
		String fedoraHostname = args[4];
		String fedoraPort = args[5];
		String fedoraUsername = args[6];
		String fedoraPassword = args[7];
		String fedoraProtocol = "http";
		
		int port = 0;
		try {
			port = Integer.parseInt(fedoraPort);
		} catch (Exception ex){
			System.out.println("The given port is not a valid port. Please use numbers. For example \"8080\"");
			return; 
		}
		
		LitsearchFedoraConnection lfc = null;
		try {
			lfc = new LitsearchFedoraConnection(fedoraHostname, port, fedoraUsername, fedoraPassword, fedoraProtocol);
		} catch (Exception e) {
			System.err.println("Fedora: " + e.getMessage());
			return;
		}
		
		MySQLConnection dbCon= MySQLConnection.getInstance(mysqlHostname,mysqlDatabase,mysqlUsername,mysqlPassword);
		Medium bean = new Medium();
		GenerateFoxml export = new GenerateFoxml();
		StringBuffer buffer = null;
		ByteArrayInputStream erg = null;
		
		
		int num=1;
		//Migrate only data that was not deleted.
		String myQuery="select id from literatur where deleted=0;";
		try {
			System.out.println("Starting with migration...\n");
			ResultSet result = dbCon.executeQuery(myQuery);
			if (result != null && result.next()) {
				do {
					Date d1 = new Date();
					long l1 = d1.getTime();
					bean.initializeMedium(result.getString("id"),mysqlHostname,mysqlDatabase,mysqlUsername,mysqlPassword);
					buffer = export.exportToFoxml(bean);
					erg = new ByteArrayInputStream(buffer.toString().getBytes("UTF-8"));
					String pid = null;
					try {
						pid = lfc.insertFedoraObject(erg);
					} catch (IOException e) {
						if(e.getMessage().startsWith("; nested exception is:")) {
							System.out.println("ERROR: Fedora Connection is not available. Abort migration.");
							return;
						} else {
							System.out.println("ERROR: " + e.getMessage());
						}
					}
					if (pid!=null) {
						Date d2 = new Date();
						long l2 = d2.getTime();
						long difference = l2 - l1;
						System.out.print("Elapsed time (msec): " + difference);
						System.out.println(" Inserted " + num + " DO with pid: " + pid + " LiteraturID: " + result.getString("id"));
					} 
					num++;
				} while (result.next());
				result.close();
			}
		} catch (NoMySQLConException e) {
			System.out.println("MySQL Error: " + e.getMessage());
			return;
		} catch (SQLException e) {
			System.out.println("SQL Error: " + e.getMessage());
			return;
		} catch(Exception e) {
			System.out.println("Fedora: " + e.getMessage());
			return;
		}
		System.out.println("\nMigration successful");
	}
}